﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MicroWebsiteSceneStr
    {
        public int Add(Chain.Model.MicroWebsiteSceneStr model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MicroWebsiteSceneStr(");
            builder.Append("OppenId,SceneStr)");
            builder.Append(" values (");
            builder.Append("@OppenId,@SceneStr)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OppenId", SqlDbType.NVarChar, 500), new SqlParameter("@SceneStr", SqlDbType.Int) };
            cmdParms[0].Value = model.OppenId;
            cmdParms[1].Value = model.SceneStr;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.MicroWebsiteSceneStr DataRowToModel(DataRow row)
        {
            Chain.Model.MicroWebsiteSceneStr str = new Chain.Model.MicroWebsiteSceneStr();
            if (row != null)
            {
                if ((row["ID"] != null) && (row["ID"].ToString() != ""))
                {
                    str.ID = int.Parse(row["ID"].ToString());
                }
                if (row["OppenId"] != null)
                {
                    str.OppenId = row["OppenId"].ToString();
                }
                if ((row["SceneStr"] != null) && (row["SceneStr"].ToString() != ""))
                {
                    str.SceneStr = Convert.ToInt32(row["SceneStr"].ToString());
                }
            }
            return str;
        }

        public bool Delete(string OppenId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteSceneStr ");
            builder.Append(" where OppenId=@OppenId");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OppenId", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = OppenId;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public Chain.Model.MicroWebsiteSceneStr GetModel(string OppenId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select top 1 *");
            builder.Append(" from MicroWebsiteSceneStr");
            builder.Append(" where OppenId=@OppenId");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OppenId", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = OppenId;
            new Chain.Model.MicroWebsiteSceneStr();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }
    }
}

